package com.hand.hzero.api.controller.v1;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import com.hand.hzero.app.service.ExampleService;
import com.hand.hzero.config.SwaggerTags;
import com.hand.hzero.domain.entity.Example;
import com.hand.hzero.domain.repository.ExampleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * API接口
 *
 * @author 13278
 */
@Api(tags = SwaggerTags.EXAMPLE)
@RestController("exampleController.v1")
@RequestMapping("/v1/example")
public class ExampleController extends BaseController {

    @Autowired
    private ExampleService exampleService;
    @Autowired
    private ExampleRepository exampleRepository;

    /**
     * 根据 id 查询
     *
     * @param id id
     * @return Example实体
     */
    @ApiOperation(value = "根据ID获取")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", paramType = "path")
    })
    @GetMapping("/{id}")
    public ResponseEntity<Example> hello(@PathVariable Long id) {
        return Results.success(exampleRepository.selectByPrimaryKey(id));
    }

    @ApiOperation(value = "插入")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PostMapping("/add")
    public ResponseEntity<Integer> add(@RequestBody Example example) {
        return Results.success(exampleRepository.insert(example));
    }

    @ApiOperation(value = "更新")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PutMapping("/update")
    public ResponseEntity<Integer> update(@RequestBody Example example) {
        return Results.success(exampleRepository.updateByPrimaryKey(example));
    }

    @ApiOperation(value = "删除")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @DeleteMapping("/{id}/delete")
    public ResponseEntity<Integer> delete(@PathVariable("id") Long id) {
        Example example = new Example();
        example.setId(id);
        return Results.success(exampleRepository.delete(example));
    }

    @ApiOperation(value = "删除2")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @DeleteMapping("/{id}/delete/dd")
    public ResponseEntity<Integer> delete2(@PathVariable("id") Long id) {
        Example example = new Example();
        example.setId(id);
        return Results.success(exampleRepository.delete(example));
    }
}